package com.tjetc.controller;

import com.tjetc.common.JsonResult;
import com.tjetc.entity.OrderRequest;
import com.tjetc.service.MerchantOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class MerchantOrderController {

    @Autowired
    MerchantOrderService merchantOrderService;

    //获得新订单列表
    @RequestMapping("merchant/getNewOrders")
    public JsonResult getNewOrders(HttpSession session){
        if(session.getAttribute("id")==null){
            return new JsonResult(1,"登录状态过期",null);
        }
        Integer merchantId = (Integer) session.getAttribute("id");
        return merchantOrderService.getNewOrders(merchantId);
    }

    //获得所有订单列表
    @RequestMapping("merchant/getAllOrders")
    public JsonResult getAllOrders(HttpSession session){
        Integer merchantId = (Integer) session.getAttribute("id");
        return merchantOrderService.getAllOrders(merchantId);
    }

        //获得订单中的菜品以及数量
        @RequestMapping("merchant/getOrderDish")
        public JsonResult getOrderDish(@RequestParam("orderId")String order, HttpSession session){
        Integer orderId= Integer.valueOf(order);
        return merchantOrderService.getDishByOrderId(orderId);
        }


        //修改订单状态为已派送
        @RequestMapping("merchant/finnishOrder")
    public  JsonResult finishOrder(@RequestParam("orderId")String order){
            Integer orderId=Integer.valueOf(order);
            return merchantOrderService.finishOrder(orderId);
        }

        @RequestMapping("merchant/getAnOrder")
        public JsonResult getAnOrder(@RequestParam("orderId")String orderId){
            Integer id=Integer.valueOf(orderId);
            return merchantOrderService.getAnOrder(id);
        }

        @RequestMapping("merchant/getOrderWith")
    public JsonResult getOrderWith(@RequestBody OrderRequest orderRequest,HttpSession session){
                Integer merchantId= (Integer) session.getAttribute("id");
                orderRequest.setMerchantId(merchantId);
                return merchantOrderService.getOrdersWith(orderRequest);
    }

    @RequestMapping("merchant/countOrdersWith")
    public  JsonResult countOrdersWith(@RequestBody OrderRequest orderRequest,HttpSession session){
        Integer merchantId= (Integer) session.getAttribute("id");
        orderRequest.setMerchantId(merchantId);
        return new JsonResult(0,"获取数量成功",merchantOrderService.countOrdersWith(orderRequest));
    }
}
